﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Microsoft.VisualBasic;
using System.Threading;
using System.Text.RegularExpressions;

namespace SNPAD
{
    public partial class ClientForm : Form
    {
        SocketClientManager _scm = null;
        string ip = "192.168.6.67";
        int port = 8501;

        bool Token = false, DispOK = false,DispNG=false  ;//通讯令牌,显示OK，显示NG 
        string SDJudge = "NG"; //松动结果
        string sBarcode = "", sSDvalue="",sBarTips="";//条码,松动值,条码说明
          
        string sPL = "",sModel="";//产线，机种
        int iCount = 0;//计数器
        enum FlowFlag
        { 
            
            未连接=0,
            连接PLC = 1,//连接PLC,sCmdReady
            等待读取条码 = 2,//等待读取条码,sCmdGetFlagA
            正在获取条码 = 4,//正在获取条码,sCmdGetBarCode
            条码判定结果写回 = 8,//将条码判定结果告诉PLC,sCmdSetFlagB2 或sCmdSetFlagB1
            等待读取松动值 = 16,//等待读取松动值，sCmdGetFlagC
            读取松动结果 = 32,//读取松动结果，sCmdGetSDValue
            读取松动判定 = 64,//读取松动判定，sCmdGetFlagE
            复位C继电器 = 128,//复位C继电器，sCmdSetFlagC
            复位A继电器 = 256,//复位A继电器，sCmdSetFlagA
            ////string Name = Enum.Parse(typeof(FlowFlag), "1").ToString();//将1转成Conn
            //Convert.ToInt32(Enum.Parse(typeof(FlowFlag), "GetFlagA"))将GetFlagA转成2
        }
        FlowFlag currFlowFlag = FlowFlag.连接PLC;//当前工作流

        const string sCmdReady = "CR\r\n";//PLC连接准备，返回“CC”表示连接成功；
        const string sCmdGetBarCode = "RDS DM5010 9\r\n";//读取条码内容

        const string sCmdGetSDValue = "RD DM5000\r\n";//读取松动值
        const string sCmdGetFlagA = "RD MR5000\r\n";//读取A继电器，返回1则说明可以取条码了，如果为0说明条码还未过来
        const string sCmdGetFlagC = "RD MR5003\r\n";//读取C继电器，返回1说明可以取松动值了，为0说明不能取
        const string sCmdGetFlagE = "RD MR5004\r\n";//松动结果判定，读取E继电器，返回1说明松动OK，为0表示NG

        const string sCmdSetFlagC = "RS MR5003\r\n";//对C继电器复位，PLC才可以存松动值
        const string sCmdSetFlagB2 = "WR MR5002 1\r\n";//条码判定OK，告诉PLC可以做松动了
        const string sCmdSetFlagB1 = "WR MR5001 1\r\n";//条码判定NG，告诉PLC报错
        const string sCmdSetFlagA = "RS MR5000\r\n";//对A继电器复位，PLC可以重新开始扫条码了
        
        const string sCmdSetCount = "WR DM5002 ";//计数器，告诉PLC通讯是正常的

        string sTips = "",sCMD ="", SavedBar="";

        public ClientForm()
        {
            InitializeComponent();
        }
        public static bool IsNumeric(string value)
        {
            return Regex.IsMatch(value, @"^([0-9.]*)$");
        }
        private void Form1_Load(object sender, EventArgs e)
        {
            //InitSocket();
            txtSend.Text = "";
            cmbPL.DataSource = OleDbHelper3.ExecuteDataTable("select * from jC_prodline");
            cmbPL.DisplayMember = "PL_Desc";
            cmbPL.ValueMember = "PL_Code";
            cmbPL.SelectedIndex = 0;
            string strPL = Interaction.GetSetting("SNPAD", "SD", "PL", "");
            if (strPL.Trim().Length > 0)
                cmbPL.SelectedValue = strPL;
            else
                cmbPL.SelectedIndex = 0;
            if(cmbPL.SelectedIndex<0)
            {
                cmbPL.SelectedIndex = 0;
                Interaction.SaveSetting("SNPAD", "SD", "PL", "");
            }

            cmbModel.DataSource = OleDbHelper3.ExecuteDataTable("select * from jC_model");
            cmbModel.DisplayMember = "model_Desc";
            cmbModel.ValueMember = "model_Code";
            cmbModel.SelectedIndex = 0;

            ip = Interaction.GetSetting("SNPAD", "SD", "PLCIP", "192.168.6.67");
            port=int.Parse( Interaction.GetSetting("SNPAD", "SD", "PLCPort", "8501"));
             
        }

        public void OnReceiveMsg()
        {
            try
            { 
                byte[] buffer = _scm.socketInfo.buffer;
                string msg = Encoding.ASCII.GetString(buffer).Replace("\0", "");
                

                if (string.Empty.Equals(msg)) return;
                if (txtMsg.InvokeRequired)
                {
                    this.Invoke(new Action(() =>
                    {
                        txtMsg.Text = AppendReceiveMsg(msg) + txtMsg.Text;

                    }));
                }
                else
                {
                    txtMsg.Text = AppendReceiveMsg(msg) + txtMsg.Text;
                }
                AnalystRevData(msg.Trim());
            }
            catch (Exception EX)
            {
                MessageBox.Show(EX.ToString());
            }
        }
        public void AnalystRevData(string msg)
        {
            try
            { 
                if (msg.ToUpper().Equals("OK"))
                     return;
                else if(msg.ToUpper().Substring(0,1).Equals("E") && msg.Length<4)
                {
                    sTips = "PLC 通讯故障：" + msg;
                    return;
                }
                if (!GlobalVar.ConnStatus) return;
                switch (currFlowFlag )
                {
                    case FlowFlag.连接PLC:
                        if (msg.ToUpper().Equals("CC"))
                        {
                            Token = false;
                            sCMD  = "PLC 连接成功"; 
                            Thread.Sleep(50);
                            currFlowFlag = FlowFlag.等待读取条码;
                            
                            _scm.SendMsg(sCmdGetFlagA);
                            sCMD = sCmdGetFlagA;
                            sTips = currFlowFlag.ToString();
                            Thread.Sleep(50);
                            Token = true;//令牌可以使用了，
                        }
                        else
                            sTips = "PLC CR指令返回错误信息："+msg;
                        break;

                    case FlowFlag.等待读取条码:
                        if (msg.Equals("1"))//允许读条码了
                        {
                            Token = false;
                            currFlowFlag = FlowFlag.正在获取条码;
                            sTips = currFlowFlag.ToString();
                            _scm.SendMsg(sCmdGetBarCode);
                            sCMD = sCmdGetBarCode;
                            Thread.Sleep(50); 
                            Token = true;//令牌可以使用了，
                        }
                        else if (msg.Equals("0"))//还不能读，继续轮询
                        {
                            Token = false; 
                            currFlowFlag = FlowFlag.等待读取条码;

                            _scm.SendMsg(sCmdGetFlagA);
                            sCMD = sCmdGetFlagA;
                            sTips = currFlowFlag.ToString();
                            Thread.Sleep(50);
                            Token = true;//令牌可以使用了，
                        } 
                        break;
                    case FlowFlag.正在获取条码:
                        if (msg.Length > 40)//条码读到了，9组ASCii码，每组两个类同“0000 0011 2233。。。。”
                        {
                            Token = true;//令牌可以使用了，

                            //处理条码
                            string[] ss = msg.Split(' ');
                            string stemp = ""; 
                            sBarcode = "";
                            for (int i=0;i<9;i++)
                            {
                               
                                if(IsNumeric(ss[i]))//如果是数值，先转成16进制
                                {
                                    stemp = Convert.ToString(Convert.ToInt32(ss[i]), 16);//to 16
                                }
                                else
                                {
                                    stemp = ss[i];
                                }
                                if (!stemp.Equals("00") && IsNumeric(stemp))
                                {
                                    if (stemp.Length >= 4)
                                    { 
                                    sBarcode += chr(Convert.ToInt32(stemp.Substring(0, 2), 16));//ASCII 转字符
                                    sBarcode += chr(Convert.ToInt32(stemp.Substring(2, 2), 16));
                                    }

                                }
                            }
                             
                            ConfirmBarcode(sBarcode );//验证条码有效性，在验证后发送相关指令
                        }
                        else
                        {
                            sTips = "PLC 读取条码：" + msg +"不符合规范";
                            lblNG.Visible = true;
                        }
                        break;
                    case FlowFlag.条码判定结果写回:
                        Token = true;//令牌可以使用了
                        sTips = "条码判定结果写回："+msg ;
                        break;
                    case FlowFlag.等待读取松动值:
                        if (msg.Equals("1"))//允许读
                        {
                            Token = false;
                            currFlowFlag = FlowFlag.读取松动结果;
                            sTips = currFlowFlag.ToString();
                            _scm.SendMsg(sCmdGetSDValue);
                            sCMD = sCmdGetSDValue;
                            Thread.Sleep(50);
                            Token = true;//令牌可以使用了，
                        }
                        else if (msg.Equals("0"))//还不能读，继续轮询
                        {
                            Token = false;
                            currFlowFlag = FlowFlag.等待读取松动值;

                            _scm.SendMsg(sCmdGetFlagC);
                            sCMD = sCmdGetFlagC;
                            sTips = currFlowFlag.ToString();
                            Thread.Sleep(50);
                            Token = true;//令牌可以使用了，
                        }
                        break;
                    case FlowFlag.读取松动结果:
                        if (IsNumeric(msg))
                        {
                            float itmp = 0;
                            if (float.TryParse(msg, out itmp))
                            {
                                sSDvalue =( itmp /1000).ToString();
                                Token = false;
                                currFlowFlag = FlowFlag.读取松动判定;
                                sTips = currFlowFlag.ToString();
                                _scm.SendMsg(sCmdGetFlagE);
                                sCMD = sCmdGetFlagE;
                                Thread.Sleep(50);
                                Token = true;//令牌可以使用了，
                            }
                            else
                            {
                                Token = false;
                                currFlowFlag = FlowFlag.等待读取松动值;

                                _scm.SendMsg(sCmdGetFlagC);
                                sCMD = sCmdGetFlagC;
                                sTips ="松动值读取错误："+ msg +"\r\n 请检查PLC";
                                Thread.Sleep(50);
                                Token = true;//令牌可以使用了，
                            }
                             
                        }
                        else
                        {
                            sTips = "松动值错误：" + msg;
                        }
                        break;
                    case FlowFlag.读取松动判定:
                        if (msg.Equals("1"))//松动OK
                        {
                            SDJudge = "OK";
                            DispOK = true;
                            DispNG = false;
                        }
                        else if (msg.Equals("0"))//松动NG
                        {
                            SDJudge = "NG";
                            DispNG = true;
                            DispOK = false;
                        }
                        SaveData();//保存数据,保存后发送复位C|A继电器指令

                        break;
                    case FlowFlag.复位C继电器:
                        Token = true;//令牌可以使用了
                        sTips = "复位C继电器错误：" + msg;
                        break;
                    case FlowFlag.复位A继电器:
                        Token = true;//令牌可以使用了
                        sTips = "复位A继电器错误：" + msg;
                        break;

                    default:
                        break;
                }
            }
            catch(Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
        }
        public void ConfirmBarcode(string barcode)
        {
            try
            {
                bool barcodeIsOk = false;
                string retmsg = "";

                string sOleDb = @"select track_status from jc_fan Where fan_code='" + barcode + "'";
                object Status = OleDbHelper3.ExecuteScalar(sOleDb, null);
                if (Status != null && !Convert.IsDBNull(Status))//有重复单号
                {
                    switch (Status.ToString().Trim())
                    {
                        case "00":
                            retmsg ="打标完成状态";
                            break;
                        case "11":
                            retmsg = "平衡测试OK状态";
                              barcodeIsOk = true ; 
                            break;
                        case "22":
                            retmsg = "松动测试OK状态";
                            barcodeIsOk = true;
                            break;
                        case "50":
                            retmsg = "SV电特测试OK状态";
                            break;
                        case "55":
                            retmsg = "绑定验证OK状态";
                            break;
                        default:
                            retmsg = "条码状态未知（"+ Status.ToString().Trim()+")";
                            break;
                    }
                }
                else//没有这个条码
                {
                    retmsg = "没有这个条码";
                }


                //以下写回判定结果
                Token = false;
                currFlowFlag = FlowFlag.条码判定结果写回;
                sTips  = currFlowFlag.ToString();
                if (barcodeIsOk)
                {
                    _scm.SendMsg(sCmdSetFlagB2);//OK，往B2继电器写
                    sCMD  = sCmdSetFlagB2;
                    //timer2.Enabled = true;
                    sBarTips = "条码："+ retmsg; 
                    DispOK = true;
                    Thread.Sleep(100);


                    currFlowFlag = FlowFlag.等待读取松动值;

                    _scm.SendMsg(sCmdGetFlagC);
                    sCMD  = sCmdGetFlagC;
                    sTips  = currFlowFlag.ToString();
                    Thread.Sleep(50);

                    Token = true;
                }
                else
                {
                    DispNG = true;
                    sBarTips = "条码错误：" + retmsg;
                    _scm.SendMsg(sCmdSetFlagB1);//NG,往B1继电器写
                    sCMD = sCmdSetFlagB1;
                    Thread.Sleep(100);
                  
                    //复位A继电器 ,重新开始
                    currFlowFlag = FlowFlag.复位A继电器;
                   sTips  = currFlowFlag.ToString();
                    _scm.SendMsg(sCmdSetFlagA);
                    sCMD = sCmdSetFlagA;
                    Thread.Sleep(100);
                    //PLCInit();

                  
                    currFlowFlag = FlowFlag.连接PLC;

                    sTips  = currFlowFlag.ToString();
                    _scm.SendMsg(sCmdReady);//连接PLC
                    sCMD  = sCmdReady; 
                     

                    Token = true;
                }
                
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
        }
        /// <summary>
        /// pLC 初始化
        /// </summary>
        public void PLCInit()
        {
            lblNG.Visible = false;
            lblOK.Visible = false;
            lblTips.Visible = true;
            currFlowFlag = FlowFlag.连接PLC;

            lblTips.Text = currFlowFlag.ToString();
            _scm.SendMsg(sCmdReady);//连接PLC
            txtSend.Text = sCmdReady;
            btnSend.Enabled = true;


        }
        public void SaveData()
        {
            try
            {
                string sOleDb = @"select fan_code from HV_SD Where fan_code='" + sBarcode  + "'";
                object sbarcode2 = OleDbHelper3.ExecuteScalar(sOleDb, null);
                if (sbarcode2 != null && !Convert.IsDBNull(sbarcode2))//以前做过
                {
                    sOleDb = "update HV_SD set pl_code='" +sPL  + "',model_code='" + sModel + "',sdvalue=" + sSDvalue + ",judge='" + SDJudge  + "',test_time=getdate() where fan_code='" + sBarcode  + "'";
                }
                else
                {
                    sOleDb = "insert  HV_SD ( fan_code,pl_code,model_code,sdvalue,judge) values('"+ sBarcode  + "','" + sPL  + "','" + sModel  + "'," + sSDvalue  + ",'" + SDJudge + "')";

                }

                if (OleDbHelper3.ExecuteNonQuery(sOleDb) <= 0)
                {
                    MessageBox.Show("数据保存失败");
                }
                else
                {
                    if(SDJudge.Trim().ToUpper().Equals("OK"))
                       sOleDb = "update jc_fan set track_status ='22',stype='HV' where fan_code='" + sBarcode + "'";
                    else
                       sOleDb = "update jc_fan set track_status ='11' ,stype='HV'where fan_code='" + sBarcode + "'";
                    OleDbHelper3.ExecuteNonQuery(sOleDb);
                    SavedBar = sBarcode; 
                }
                ////以下复位C继电器
                //Token = false;
                //currFlowFlag = FlowFlag.复位C继电器;
                //sTips   = currFlowFlag.ToString();
                //_scm.SendMsg(sCmdSetFlagC);
                //sCMD  = sCmdSetFlagC;
                //Thread.Sleep(400);

                //复位A继电器
                Token = false;
            
                currFlowFlag = FlowFlag.复位A继电器 ;
                sTips  = currFlowFlag.ToString();
                _scm.SendMsg(sCmdSetFlagA);
                sCMD  = sCmdSetFlagA;
                Thread.Sleep(100);
                //执行两次 
                _scm.SendMsg(sCmdSetFlagA); 
                Thread.Sleep(100);
                Token = true;

                currFlowFlag = FlowFlag.等待读取条码;
                sTips = "保存成功," + currFlowFlag.ToString();
                _scm.SendMsg(sCmdGetFlagA );
                sCMD = sCmdGetFlagA;
                Thread.Sleep(100);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
        }
        public void OnConnected()
        {
            if (txtMsg.InvokeRequired)
            {
                this.Invoke(new Action(() => {
                    txtMsg.Text = GetDateNow() + "  " + "连接PLC IP:" + ip + " : " + port + "成功\r\n" + txtMsg.Text;
                    string ipClient = _scm._socket.LocalEndPoint.ToString().Split(':')[0];
                    string posrClient = _scm._socket.LocalEndPoint.ToString().Split(':')[1];
                    lblClientIps.Text = ipClient;
                    lblClientPorts.Text = posrClient;
                    lblRemoteIps.Text = ip;
                    lblRemotePorts.Text = port.ToString();
                    txtPort.Text = port.ToString();
                    txtIP.Text = ip;

                    lblStatuss.Text = "正常";
                    txtSend.Text = "";
                    GlobalVar.ConnStatus = true;
                    PLCInit();
                    


                }));
            }
            else
            {
                txtMsg.Text = GetDateNow() + "  " + "连接PLC IP:" + ip + " : " + port + "成功\r\n" + txtMsg.Text;
                GlobalVar.ConnStatus = true;
                PLCInit();
            }
        }

        public void OnFaildConnect()
        {
            GlobalVar.ConnStatus = false;
            if (txtMsg.InvokeRequired)
            {
                this.Invoke(new Action(() =>
                {
                    txtMsg.Text = GetDateNow() + "  " + "连接PLC IP: " + ip + " : " + port + " 失败\r\n"+ txtMsg.Text ;
                    lblStatuss.Text = "连接失败";
                    btnConnected.Enabled = true;
                }));
            }
            else
            {
                btnConnected.Enabled = true;
                txtMsg.Text = GetDateNow() + "  " + "连接PLC IP:" + ip + " : " + port + " 成功\r\n" + txtMsg.Text;

                PLCInit();
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            _scm.SendMsg(txtSend.Text+"\r\n");
            txtMsg.Text = AppendSendMsg(txtSend.Text) + txtMsg.Text;
            txtSend.Text = "";
        }

        public string AppendSendMsg(string msg)
        {
            return GetDateNow() + "  " + "[发送]  " + msg + "\r\n";
        }

        public string AppendReceiveMsg(string msg)
        {
            return GetDateNow() + "  " + "[接收]  " + msg + "\r\n";
        }

        public string GetDateNow()
        {
            return DateTime.Now.ToString("HH:mm:ss");
        }

        private void InitSocket()
        {
            lblTips.Visible = false;
            _scm = new SocketClientManager(ip, port);
            _scm.OnReceiveMsg += OnReceiveMsg;
            _scm.OnConnected += OnConnected;
            _scm.OnFaildConnect += OnFaildConnect;
            lblStatuss.Text = "连接中，请稍候...";
            btnConnected.Enabled = false;
            lblTips.Text = "";
            txtSend.Text ="";
            _scm.Start();
        }

        private void btnConnected_Click(object sender, EventArgs e)
        {
            InitSocket();
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            GlobalVar.ConnStatus = false;
            btnConnected.Enabled = true;
            currFlowFlag = FlowFlag.未连接;
            if (!_scm._socket.Connected) return;
            _scm._isConnected = false;
            //_scm.SendMsg("\0\0\0faild");
            _scm._socket.Shutdown(System.Net.Sockets.SocketShutdown.Both);
            _scm._socket.Close();
            lblClientIps.Text = "";
            lblClientPorts.Text = "";
            lblRemoteIps.Text = "";
            lblRemotePorts.Text = "";
            lblStatuss.Text = "未连接";
            txtMsg.Text = "连接已经断开\r\n" + txtMsg.Text;
           
        }

        private void button2_Click(object sender, EventArgs e)
        {
            txtSend.Text = "";
            txtMsg.Text = "";
        }
         

        private void ClientForm_FormClosing(object sender, FormClosingEventArgs e)
        {
        //    Interaction.SaveSetting("SNPAD", "SD", "PL", cmbPL.SelectedValue.ToString());
        //    Interaction.GetSetting("SNPAD", "SD", "PLCIP", ip);
        //    Interaction.GetSetting("SNPAD", "SD", "PLCPort", port.ToString());
        }

        private void txtPort_TextChanged(object sender, EventArgs e)
        {

        }

        private void skinButton2_Click(object sender, EventArgs e)
        {
           

        }

        private void txtIP_TextChanged(object sender, EventArgs e)
        {

        }

        private void lblTips_Click(object sender, EventArgs e)
        {

        }
        /// <summary>
        /// 连接PLC，每秒写一次
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void timer1_Tick(object sender, EventArgs e)
        {
            
       
             if (txtSend.Text.Length > 10000) txtSend.Text = "";
            
            if (!lblTips.Text.Equals(sTips))
            {
                lblTips.Text = sTips;
                 
            }
            if(!sBarTips.Equals(lblBarTip.Text ))
            {
                lblBarTip.Text = sBarTips; 
            }
            if (!sCMD.Equals(txtSend.Text))
            {
                txtSend.Text = sCMD; 
            }
            if (!sBarcode.Equals(lblBarcode.Text))
            {
                lblBarcode.Text = sBarcode; 
            }
            if(!sSDvalue.Equals(lblSDValue.Text) )
            {
                lblSDValue.Text = sSDvalue; 
            }

            if(!sPL.Equals(cmbPL.SelectedValue.ToString()))
            {
                sPL = cmbPL.SelectedValue.ToString();
            } 
            if (  !sModel.Equals(cmbModel.SelectedValue.ToString()))
            {
                sModel = cmbModel.SelectedValue.ToString();
            }

            if (DispNG )
            {
                lblNG.Visible = true;
                lblOK.Visible = false;
            }
            else if(DispOK )
            {
                lblNG.Visible = false ;
                lblOK.Visible = true;
            }
            if(SavedBar.Length>0)
            {
                
                listBox1.Items.Add(SavedBar+" , "+ sSDvalue  );
                SavedBar = "";
            }
            if (!GlobalVar.ConnStatus) return;

            if (!Token) return;


            iCount = (iCount + 1) % 65535;
            if (iCount % 2 == 0)
            {//// _scm.SendMsg(sCmdSetCount.ToString() + Convert.ToString(iCount , 16)  + "\r\n");//转换成16进制
                _scm.SendMsg(sCmdSetCount.ToString() + iCount + "\r\n");//转换成16进制
                txtSend.Text = sCmdSetCount.ToString() + iCount + "\r\n";
                Thread.Sleep(50);
                if(sCMD.Length>0)  _scm.SendMsg(sCMD );//同时发一次命令

            }

        }


        private void skinButton1_Click(object sender, EventArgs e)
        {
            if (GlobalVar.CurrOperator.ToUpper().Equals("MANAGER"))
            { 
            Interaction.SaveSetting("SNPAD", "SD", "PL", cmbPL.SelectedValue.ToString());
            Interaction.GetSetting("SNPAD", "SD", "PLCIP", ip);
            Interaction.GetSetting("SNPAD", "SD", "PLCPort", port.ToString());
            }
            else
            {
                MessageBox.Show("只有管理员用户MANAGER才能使用");
            }
        }

     
        private void skinButton2_Click_1(object sender, EventArgs e)
        {
            int i = Convert.ToInt32("1f", 16);//to 10
            string ss = Convert.ToString(12336, 16);//to 16
            string sa = chr(34);

            ss = chr(Convert.ToInt32("3236".Substring(0, 2),16));
            ss = chr(Convert.ToInt32("3236".Substring(2, 2),16));
            ss = chr(36);

        }

        public string chr(int asciicode)
        {
            if(asciicode >=0 && asciicode<=255)
            {
                System.Text.ASCIIEncoding asciiEncodeing = new System.Text.ASCIIEncoding();
                byte[] byteArray = new byte[] { (byte)asciicode };
                string strChar = asciiEncodeing.GetString(byteArray);
                return (strChar);

            }
            else
            {
                return "error";
            }
        }
    }
}
